How to POST direct message using Twitter REST API in SWIFT 4

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to POST direct message using Twitter REST API in SWIFT 4



I want to implement to send direct message to my twitter friends using Twitter REST API. I am getting all my friends id and put invite button for sending a message from my app.



Following is the function call I am using -


func sendInviteMessage(_ client: TWTRAPIClient, recepientID: String)

let statusesShowEndpoint = " https://api.twitter.com/1.1/direct_messages/events/new.json"
let params = ["recipient_id": recepientID,"message_data":"Hello","type":"message_create"]
var clientError : NSError?

let request = client.urlRequest(withMethod: "POST", urlString: statusesShowEndpoint, parameters: params, error: &clientError)

client.sendTwitterRequest(request) (response, data, connectionError) -> Void in
if connectionError != nil
print("Error: (String(describing: connectionError?.localizedDescription))")


do
let json = try JSONSerialization.jsonObject(with: data!, options: )
print("json: (json)")

catch let jsonError as NSError
print("json error: (jsonError.localizedDescription)")






When I tap invite button it gives me unsupported url response.





no one understrand the required parameter until you show your tried code
– V_rohit
Aug 6 at 9:08





Required parameters to be send
– iHarshad
Aug 6 at 9:11





@V_rohit - Can u please help me on this
– iHarshad
Aug 6 at 10:15





Pro tip: it looks like most of your questions are this sort of length, and they are just not a good fit for Stack Overflow. We expect questions to set out a problem in detail, and to show something that was tried, or some other prior research/effort. You may find that if a number of your earlier (off-topic) questions are put on hold - and many of them should be - an automatic question ban may be applied to your account.
– halfer
Aug 6 at 10:16






In this case, you could put your title into a search engine and research the problem yourself. You could then try piece of code, make an effort to understand how they work, and ask a question if you run into a problem.
– halfer
Aug 6 at 10:17




1 Answer
1



params is wrong. And you can use MutableURLRequest.


params



https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event#example-request


let params = [
"event": [
"type":"message_create",
"message_create": [
"target": ["recipient_id": recepientID],
"message_data": ["text": "Hello"]
]
]
]
var request = client.urlRequest(withMethod: "POST",
urlString: statusesShowEndpoint,
parameters: nil,
error: &clientError)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: )





Ah, setting parameters: to nil and using request.httpBody really helped me out here. Thanks!
– Bellovic
Sep 19 at 13:56






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard