How to send HTTP body plain text for GET method?
Asked Answered
E

2

7

I had a problem, I use the iGDB REST API which need to send some plain text for some endpoints with GET method.

There is no problem with PostMan (by selecting "Body" > "raw" & paste my query), but when I try with Objective-C, an error appear telling me "GET method must not have a body"...

PostMan example

Here is the code used in my app:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_URL"]];
[request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"GET"];
[request setHTTPBody:[@"sort popularity desc;" dataUsingEncoding:NSUTF8StringEncoding]];

EDIT 02/10/2019

Trying to add each filters in headers not working...

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_URL"]];
[request setValue:[[@"id,name,first_release_date,release_dates,cover,platforms" dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0] forHTTPHeaderField:@"fields"];
[request setValue:[[@"popularity desc" dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0] forHTTPHeaderField:@"sort"];
[request setValue:[[@"5" dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0] forHTTPHeaderField:@"limit"];
[request setValue:[[@"0" dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0] forHTTPHeaderField:@"offset"];

Thanks in advance for any replies !

Essy answered 2/10, 2019 at 13:10 Comment(1)
Would love to know how you fixed this since I'm working with the same issue.Rabjohn
R
7

It's no longer possible to send a GET request with a body, you'll have to send the data via the query string of the URL, either by building the string manually or with the help of NS(Mutable)URLComponents.


As per the iOS 13 release notes, GET requests are no longer allowed to have a body:

All URLSessionTask instances with a GET HTTP method that contain a body now produce the error NSURLErrorDataLengthExceedsMaximum. (46025234)

This makes URLSession more conformant with the HTTP/1.1 RFC:

A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests.

Ripe answered 2/10, 2019 at 18:13 Comment(0)
C
0

If I get true your question, you can set parameters to your request header when you get you can set like below.

  [request addValue:@"valueForKey1" forHTTPHeaderField:@"key1"];

  [request addValue:@"valueForKey2" forHTTPHeaderField:@"key2"];

If you send a data format so,

NSString *stringValueOfParameters =[NSString stringWithFormat:@"userName:blabla"];
NSData *convertedDat=[stringValueOfParameters dataUsingEncoding:NSUTF8StringEncoding];
NSString *headerValue=[NSString stringWithFormat:@"Basic %@",[convertedDat base64EncodedStringWithOptions:0]];

[request setValue:headerValue forHTTPHeaderField:@"headerKey"];

// Edit

You must try kinda like that, directly write a Dictionary or like below write your JSON parameters as NSString then convert it to Data.

NSDictionary *dictParams = @{ @"fields" : @[@"id",@"name",@"first_release_date",@"release_dates",@"cover",@"platforms"], @"sort" : @"popularity desc", @"limit": @5, @"offset":@0 };

// this is taken from an example

NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":65,\"type\":\"text\"}}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

Then try it to set header.

Chromic answered 2/10, 2019 at 13:15 Comment(3)
Thanks for your response ! But it seems not work ... My full payload is "fields id,name,first_release_date,release_dates,cover,platforms; sort popularity desc; limit 10; offset 0;" but when I try to add a parameter for each, the response data is not affected...Essy
hey @Essy can you look at my updated answer, try the code all parameters in a string then convert it to stringencoding data format.Chromic
Thanks @elia, but it's not working too, please check my edited post to see if I do it correctly :).Essy

© 2022 - 2024 — McMap. All rights reserved.