AFHTTPRequestOperationManager custom http header always start with "HTTP_"? - ios

I use AFHTTPRequestOperationManager to send HTTP request, and have some information put in the HTTP custom header, named "X-AKey".
I confirmed the header name by:
NSLog(#"%#", manager.requestSerializer.HTTPRequestHeaders);
Then, I captured the outgoing message, and found that the header name has changed to "HTTP_X_AKEY".
I've searched some questions, and found that this might be a standard way of naming custom header: "Meta-variables with names beginning with "HTTP_" contain values read from the client request header fields, if the protocol used is HTTP. The HTTP header field name is converted to upper case, has all occurrences of "-" replaced with "" and has "HTTP" prepended to give the meta-variable name."
Nevertheless, my question is: CAN I send a message with a custom header name exactly as I specified? That is, in my case, "X-AKey" instead of "HTTP_X_AKEY"?
(PS: For real applications, if both the client and server are negotiated to use the 'HTTP_xxx' format, there won't be any problem. I'm just curious about the answer for now.)
Thanks.

Try this:
[manager.requestSerializer setValue:#"SomeValue" forHTTPHeaderField:#"SomeHeaderField"]

Related

Header HTTP Origin in TWebRequest

How can I get the equivalent of php:
$_SERVER['HTTP_ORIGIN']
in delphi?, I'm using TWebRequest in a WebBroker applications project (not datasnap).
It works using Request.GetFieldByName ('ORIGIN') where Request is some instance of TWebReques. However, there are "hidden" headers, to obtain them you can use a solution based on the response of the following link: Enumerate TWebRequest HTTP header fields, and save them in a TDictionary

Is there a default media type for GET and other methods

Is there any default media type when the query is not specified with any supported media types in RESTCONF ?
No. There is no standard default. This is server implementation dependent, so do not rely on it.
From draft-ietf-netconf-restconf-17, Section 5.3, Message Encoding:
The server MUST support the "Accept" header field and "406 Not
Acceptable" status-line, as defined in [RFC7231]. The response
output content encoding formats that the client will accept are
identified with the Accept header field in the request. If it is not
specified, the request input encoding format SHOULD be used, or the
server MAY choose any supported content encoding format.
If there was no request input, then the default output encoding is
XML or JSON, depending on server preference. File extensions encoded
in the request are not used to identify format encoding.
And from draft-ietf-netconf-restconf-17, Section 7.1, Error Response Message:
The client SHOULD specify the desired encoding(s) for response
messages by specifying the appropriate media-type(s) in the Accept
header. If the client did not specify an Accept header, then the
same structured syntax name suffix used in the request message SHOULD
be used, or the server MAY choose any supported message encoding
format. If there is no request message the server MUST select
"application/yang-data+xml" or "application/yang-data+json",
depending on server preference.
The final RFC stood by the draft, just as #predi said:
On Message Encoding, Section 5.2:
If there was no request input, then the default output encoding is
XML or JSON, depending on server preference. File extensions encoded
in the request are not used to identify format encoding.
And Error Message Response, Section 7.1
If the client did not specify an "Accept" header, then the same
structured syntax name suffix used in the request message SHOULD be
used, or the server MAY choose any supported message-encoding
format. If there is no request message, the server MUST select
"application/yang-data+xml" or "application/yang-data+json",
depending on server preference.

How to consume HTTPS webservice having wsdl and soap12 version.

While sending soap1.2 request what are the namespaces we need to use in soapbody? Any sample code available in ios programming?
SOAP request namespace start with following prefix.
http://www.w3.org/2003/05/
And depending on the type of element there are suffix. so for eg.
Envelope - http://www.w3.org/2003/05/soap-envelope
Addressing Header - http://www.w3.org/2005/08/addressing
Check This example.

AFNetworking content type for HEAD request for ios8

I have a HEAD request to certain image with If-modified-since HTTP header, in order to check if image is actually modified. If modified, then i just send GET request for this image.
It used to work fine for iOS 7, And it works now for iOS 7. But for iOS 8 i got
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: image/png"
So the same code works for ios7 and for ios8- don't. Any ideas?
First, you don't need to make a HEAD request. An appropriate NSURLRequestCachePolicy can accomplish the same thing.
Second, the error is caused by an unacceptable content type being sent by the response for the response serializer. For image/png, use AFImageResponseSerializer, or set the appropriate acceptableContentTypes property for the response serializer of your choice.
Thanks Matt for showing the way. I removed HEAD request. Now i use AFHTTPRequestOperation with AFImageResponseSerializer. Also i set 'If-Modified-Since' header in NSMutableURLRequest with setValue:forHTTPHeaderField method. This date is stored in userdefaults, where key is URL, and value is the date where this URL was modified.
I'm sure there should be much better way for this, like using NSURLCache, but i couldn't do it.

iOS / Obj-c - Get HTTP status code response message (reason phrase)

I'm working with an API that sends back HTTP 406's for many different errors, along with a custom message (reason phrase). It may look something like:
406 Not Acceptable: User is already logged in
406 Not Acceptable: Missing password field
406 Not Acceptable: Node does not exist.
I can get the 406 status code and the standard "Not Acceptable" string using:
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];
[NSHTTPURLResponse localizedStringForStatusCode:HTTPResponse.statusCode];
However I really require the reason phrase message to know how to handle the response. How can I get it, preferably using the standard iOS SDK?
I really require the reason phrase message to know how to handle the response.
Then the API is broken. The reason phrase is a debugging aid only. It's not meant to inform client behaviour.
From RFC 2616 ยง 6.1.1:
The Status-Code is intended for use by automata and the Reason-Phrase is intended for the human user. The client is not required to examine or display the Reason- Phrase.
If there is information about the response that cannot be conveyed by the status code alone, the proper place for it is as a header or in the response body. The reason phrase is not a correct place to put information necessary for a client to use.
Status code 406 means that the server cannot respond with the accept-header specified in the request.
406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
These error codes are not iOS-specific. If you want to display different messages based on different occurrence reasons, I suppose you should check it with your API/web server, and use conditions in your code to display your custom messages for each of them.
Ultimately, you can get the reason phrase using the ASIHTTPRequest library.
http://allseeing-i.com/ASIHTTPRequest/
It was just as simple to use in my case as AFNetworking and NSURLSession.

Resources