0

I do not know how to add parameters to the post request to Overpass-API. To create request I am using AFNetworking.

1) at the beginning, i build query in overpass-turbo.eu than i export it to XML, so i get this

<osm-script> <query type="node"> <has-kv k="amenity" v="drinking_water"/> <bbox-query e="12.51119613647461" n="41.89248629819397" s="41.88659196260802" w="12.488558292388916"/> </query> <print/> </osm-script>

and put that in NSString

NSString *myString = @"<osm-script><query type=\"node\"><has-kv k=\"amenity\" v=\"drinking_water\"/><bbox-query e=\"12.51119613647461\" n=\"41.89248629819397\" s=\"41.88659196260802\" w=\"12.488558292388916\"/></query><print/></osm-script>";

2) than i try build params AFNetworking - How can I PUT and POST raw data without using a key value pair?

So i create the NSData category method base64DataFromString is available here.

And create my request

NSString *urlString = @"http://overpass-api.de/api/interpreter";
NSURL *myUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];

// Generate an NSData from your NSString (see below for link to more info)
NSData *postBody = [NSData base64DataFromString:myString];

// Add Content-Length header if your server needs it 
unsigned long long postLength = postBody.length;
NSString *contentLength = [NSString stringWithFormat:@"%llu", postLength];
[request addValue:contentLength forHTTPHeaderField:@"Content-Length"];

// This should all look familiar...
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response data: %@", responseObject);
        self.textView.text = responseObject;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        self.textView.text = [NSString stringWithFormat:@"%@",error];
    }];
[manager.operationQueue addOperation:operation];

3) But now i get error

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x1700fc680 {NSUnderlyingError=0x170247290 "Request failed: unacceptable content-type: text/html", com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x170230860> { URL: http://overpass-api.de/api/interpreter } { status code: 400, headers { Connection = close; "Content-Encoding" = gzip; "Content-Length" = 491; "Content-Type" = "text/html; charset=utf-8"; Date = "Sat, 13 Dec 2014 13:25:37 GMT"; Server = "Apache/2.2.22 (Ubuntu)"; Vary = "Accept-Encoding"; } }, NSErrorFailingURLKey=http://overpass-api.de/api/interpreter, com.alamofire.serialization.response.error.data=<...

What im doing wrong?

Community
  • 1
  • 1
oscarr
  • 529
  • 5
  • 6
  • 1
    These are HTTP parameters, right? – Droppy Dec 08 '14 at 15:14
  • 1
    Can you be more specific? What parameters do you want to add and where? – scai Dec 08 '14 at 16:30
  • I found this example and i want to do it the same on ios http://stackoverflow.com/questions/15719398/overpass-api-android-example but i am not sure what params i need to send it – oscarr Dec 09 '14 at 16:00

1 Answers1

1

This what you need to achieve is to create request with xml as the body. In your example AFNetworking will convert parameters into some body, but that body won't be an xml. You should store your xml as string and send it as raw body data: AFNetworking - How can I PUT and POST raw data without using a key value pair?

Community
  • 1
  • 1
Kendzi
  • 106
  • 1
  • 10