I need a simple example where we POST some HTTP request to the server. And also i need to add some headers to that HTTP post. I have tried posting the HTTP post, but little confused about adding headers in the request. Is there any mandatory field/HEADER i have to add in the HTTP Post ??
Asked
Active
Viewed 2,227 times
3
-
You mean , the content-length is mandatory ? – Cyril Jan 31 '12 at 13:49
-
1Depending on exactly what you're trying to send, it may well be. You could POST form values, multipart messages or even several things in succession (like from a streaming response, where you don't know the length up front). – Jesper Jan 31 '12 at 16:10
-
Any example for Http Post where i can add the headers also in that ?? – Cyril Feb 01 '12 at 07:39
-
As I said, it depends on how you're passing the values. The accepted answer tells you how to set the header, but the way you're passing the POST data will answer what to send. In most cases it's the number of bytes in the binary data you're posting, but I encourage you to find out specifically how you're sending data so that you can work this out for yourself. Since you didn't describe your scenario, I can't tell you what will work. – Jesper Feb 02 '12 at 08:58
1 Answers
1
Suppose you have an NSURLRequest
called theRequest
.
You configure the request type to POST with
[theRequest setHTTPMethod:@"POST"];
And for the headers, you add values to the Header you add them as key value pairs:
[theRequest setValue:authorizationString forKey:@"Authorization"];
which sets authorizationString
for the Authorization
header.

Abizern
- 146,289
- 39
- 203
- 257
-
Is there any example for HTTP post along with adding the headers with the post ? can u give me ? – Cyril Feb 01 '12 at 07:38