2015-04-02 126 views
0

我试图从我的iOS应用程序创建订单到我的Shopify网站。Shopify创建订单

这是文档说我应该做的。

使用产品变体ID创建简单订单。 POST /admin/orders.json { "order": { "line_items": [ { "variant_id": 447654529, "quantity": 1 } ] } }

它不多说了。

这是我得到的。

<code> 
NSMutableDictionary *lineItem1=[[NSMutableDictionary alloc]init]; 
[lineItem1 setObject:@"1125533997" forKeyedSubscript:@"variant_id"]; 
[lineItem1 setObject:@"1" forKeyedSubscript:@"quantity"]; 

NSMutableArray *lineItems=[[NSMutableArray alloc]init]; 
[lineItems addObject:lineItem1]; 


NSMutableDictionary *orders=[[NSMutableDictionary alloc]init]; 
[orders setObject:lineItems forKeyedSubscript:@"line_items"]; 


NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:orders options:NSJSONWritingPrettyPrinted error:&error]; 

NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

myString=[JuicyApi md5HexDigest:myString]; 


//Set parameter 
NSMutableDictionary *params = [[NSMutableDictionary alloc]init]; 
[params setObject:myString forKeyedSubscript:@"order"]; 

//Generate the request with the give settings 
NSMutableURLRequest *req = [self getRequestWithFunction:@"admin/orders.json" requestType:@"POST" params:params ssl:true]; 

</code> 

服务器给我的回复说。

<code> 
{"errors":{"order":"expected String to be a Hash"}} 
</code> 

我试着散列所有它,只有值,在这个例子中的一切顺序,无法让它的工作。我是否错误地哈希?

我在这里错过了什么?

+0

我碰到过这个寻找相同的解决方案。我现在修好了。它与哈希无关,该错误是误导。不要从您的JSON创建一个哈希字符串。只需发布JSON并确保您的JSON有效并且没有任何额外的转义。那个哈希字符串错误将会消失,并且它会工作。从你的代码中,我会建议创建一个包含“order”的字典,然后将其转换为JSON字符串一次。如果您先将字典的子部分转换为JSON,那么父代又会创建额外的转义,这在我的例子中引起了这个问题。 – 2015-04-05 23:01:13

回答

0

我也有同样的问题。我已经通过设置正确的标题在提出请求之前得到修复 setHeader(“Content-Type”,“application/json”)