0

Is there any way to send POST data in GET request using ASIHTTPRequest or any other networking library in iOS?

akshay1188
  • 1,647
  • 2
  • 17
  • 35
  • What kind of POST data? Do you want to set the Request body? –  Oct 22 '12 at 11:31
  • POST and GET are methods of HTTP, if you POST something you fil the HTTP body with parameter. When you GET something you would use URL query string to set some parameters. If you want to mix the two use POST since that will allow you to set parameters in the HTTP body and add URL query string parameters. – rckoenes Oct 22 '12 at 11:34
  • I want to attach JSON data to the request body – akshay1188 Oct 22 '12 at 11:36
  • Anything like this http://stackoverflow.com/questions/2064281/sending-post-data-with-get-request-valid – akshay1188 Oct 22 '12 at 11:37
  • 1
    http://www.whathaveyoutried.com – deanWombourne Oct 22 '12 at 11:38
  • I have tried to attach it as GET params, but that does not work. Also, cannot attach the JSON in header as theres no key against which that can be attached – akshay1188 Oct 22 '12 at 11:48

1 Answers1

1

Adding POST data to a GET request doesn't really make that much sense - some proxies won't like it and some webservers won't accept it or will mangle the data on the way in. however, I guess there's a few places to start if you're confident it will work with your server :

Searching for the word POST in the ASI docs will show you how using the ASIHTTPRequest object. However, ASI is deprecated and other solutions should be used.

Searching stack overflow gives you this question which shows you how to add POST data to NSURLRequest

Community
  • 1
  • 1
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • The webserver is desgined to work that way. The corresponding cURL script works. Sample format - curl -i --header "Content-Type: application/json" --header "Accept: application/json" --header "client-agent: 1.21.2" -X GET --data "{"authtoken":"lfghaskjhgh","selection":"value"}" http://serveraddress – akshay1188 Oct 22 '12 at 11:50
  • just not sure how to pass that JSON in --data to GET request – akshay1188 Oct 22 '12 at 12:02
  • Have you tried using `ASIFormDataRequest` (see the docs I linked to in my answer), adding the POST data as per the example and then setting the request type to GET like `myRequest.`requestMethod = @"GET";` ? – deanWombourne Oct 22 '12 at 12:35
  • Yes. I tried that. When I use setPostValue:forKey: I get a 405 Method Not Allowed. – akshay1188 Oct 22 '12 at 13:52
  • That's from your server, not from ASI - what get sent over the wire to the server? Did the post data get added or sent at all? – deanWombourne Oct 22 '12 at 14:12
  • Yes. Thats from the server. I used HTTPScoop and it shows the JSON in POST Data of the request. I guess it is not possible. Just came across this https://groups.google.com/forum/?fromgroups=#!topic/asihttprequest/kY8R8RlqO3Q – akshay1188 Oct 22 '12 at 14:28
  • First you say it's sending the POST data and then you say it's not possible? If HTTPScoop says the JSON went from the device to the server, surely it's a problem on the server side? – deanWombourne Oct 22 '12 at 14:51
  • The JSON went through, but the server is not accepting it. Because although I am setting requestMethod to @"GET", it is showing me as POST in the scoop. – akshay1188 Oct 22 '12 at 15:11
  • Can you subclass `ASIFormDataRequest` to always pass `GET` as the method? – deanWombourne Oct 22 '12 at 16:44