Jump to content

Send post to the web services / api [Orders]


Recommended Posts

Hello,

 

I am using the Prestashop web service / API to build an iOS application.

 

The application have to be able to do orders ...

 

From other links I found some interesting informations but I still can't figure out.

 

From this link http://www.prestashop.com/forums/topic/241368-webservice-erreur-xml-de-type-127-string-could-not-be-parsed-as-xml/ (in french) it is said that we have to send the order through parameter "xml".

 

But still don't working ...

 

Url :

http://WS-KEY:@HOST/api/carts?xml=<prestashop xmlns:xlink="http://www.w3.org/1999/xlink"><cart><id></id><id_address_delivery>5</id_address_delivery><id_address_invoice>5</id_address_invoice><id_currency>1</id_currency><id_customer>2</id_customer><id_guest>0</id_guest><id_lang>2</id_lang><id_shop_group>0</id_shop_group><id_shop>1</id_shop><id_carrier>0</id_carrier><recyclable></recyclable><gift>0</gift><gift_message></gift_message><mobile_theme></mobile_theme><delivery_option></delivery_option><secure_key></secure_key><allow_seperated_package></allow_seperated_package><date_add></date_add><date_upd></date_upd><associations><cart_rows><cart_row><id_product>2</id_product><id_product_attribute>18</id_product_attribute><quantity>42</quantity></cart_row></cart_rows></associations></cart></prestashop>

 

Error:

 

Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x16639140 {NSUnderlyingError=0x16638630 "bad URL", NSLocalizedDescription=bad URL

 

 

Why it is not possible to send those data (order) into the BODY request ?

 

 

When sending data into the BODY request I get:

 

Error:

 

String could not be parsed as XML

XML length : 0

Original XML :

 

Thanks for helping

Edited by adril (see edit history)
Link to comment
Share on other sites

Hey I just figure out, here the solution:

 

+ (void)sendCart:(Cart *)cart success:(void (^)(NSMutableDictionary *result))success failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure {

[AppDelegate appDelegate].prestashopAPI.succesCartsHandler = success;

//INFO: define the route
NSString *routeString = @"carts";

NSLog(@"%s | routeString: %@", __PRETTY_FUNCTION__, routeString);

//INFO: define the requeste
NSMutableURLRequest *request = [sessionManager requestForRoute:routeString];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];


NSString *xmlString = [NSString stringWithFormat:@"xml=%@", [Cart convertCartToXML:cart] ];
NSLog(@"%s | xmlString: %@", __PRETTY_FUNCTION__, xmlString);
[request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%s | Request body A %@", __PRETTY_FUNCTION__, [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
NSLog(@"%s | %@", __PRETTY_FUNCTION__, [request description]);

//INFO: start parsing
[PrestashopAPI parse:request success:success failure:failure];
}

 

Here it is important to set the Header values:

- Content-Type with application/x-www-form-urlencoded

- Accept with application/xml

- add "xml=" before your xml data to post

 

After that you can set your item/object to send in the HTTP BODY request as NSData.

 

Exemple with a Cart:

 

<prestashop url="" xmlns:xlink="http://www.w3.org/1999/xlink"><cart><id></id><id_address_delivery>5</id_address_delivery><id_address_invoice>5</id_address_invoice><id_currency>1</id_currency><id_customer>2</id_customer><id_guest>0</id_guest><id_lang>2</id_lang><id_shop_group>0</id_shop_group><id_shop>1</id_shop><id_carrier>0</id_carrier><recyclable></recyclable><gift>0</gift><gift_message></gift_message><mobile_theme></mobile_theme><delivery_option></delivery_option><secure_key></secure_key><allow_seperated_package></allow_seperated_package><date_add></date_add><date_upd></date_upd><associations><cart_rows><cart_row><id_product>2</id_product><id_product_attribute>18</id_product_attribute><quantity>42</quantity></cart_row></cart_rows></associations></cart>[/b]</prestashop[>

Edited by adril (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...