0

I have created an Email utility with the help of Google Endpoints (Python). The problem is that I am not able to POST data to it. I am getting an error.

{ "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } }

What I am doing in Objective-C is given below

//Here my URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];


//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];

//Pass The String to server
NSString *dataPost = @"{\"body\": \"email body\",\"subject\": \"email subject\"}";

//Check The Value what we passed
NSLog(@"the data Details is =%@", dataPost);

//Convert the String to Data
NSData *data1 = [dataPost dataUsingEncoding:NSUTF8StringEncoding];

//Apply the data to the body
[request setHTTPBody:data1];

//Create the response and Error
NSError *err;
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

//This is for Response
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
    NSLog(@"got response");
    /* ViewController *view =[[ViewController alloc]initWithNibName:@"ViewController" bundle:NULL];
     [self presentViewController:view animated:YES completion:nil];*/
}
else
{
    NSLog(@"faield to connect");
}
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100

1 Answers1

1

Please see: How to send json data in the Http request using NSURLRequest

Basically, you need to set the "Content-Type" header so that the receiving endpoint doesn't think it's form encoded (the default) and instead recognizes it as JSON.

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
Community
  • 1
  • 1
maxjar10
  • 216
  • 1
  • 4
  • its working fine with this data @"{\"body\": \"test\",\"subject\": \"test\"}" but it is not working with this type of data {"body": "body First Name Paul A Alabisi Last Name Customer ID 31877 Serail Key 2512948ead29a82d","subject": "sub"} – Vikas Bansal Sep 11 '15 at 05:55