使用CURL- Post方法和json调用端点已损坏

时间:2019-02-25 15:14:16

标签: json curl quotes

我正在通过以下命令使用CURL调用我的端点:

curl -H "Content-Type: application/json"  -X POST  -d  '{"TransmissionID":"SO000001","CustomerSO":"SO000001","EndUserName":"Roi_Test","Hold":"","RequestedDate":"2019-02-24 15:00","Currency":"EUR","Address1":"Calle del Maestro Bagant","Address2":"","BuildingName":"","BuildingNumber":"1","Floor":"1","ContactPerson":"roi","City":"Valencia","CountryAbbriviation":"ES","Email":"aaa@joomi.co.il","Phone":"050-7680249","Zip":"46015","Remark":"","Incoterm":"DDP","Status":"","Item":["OrderLine":1,"ItemName":"cl111","ItemDescription":"Description","Quantity":"1.","PriceCurrency":"EUR","Price":"219.9","HSCode":"9900000003463","AWBNumber":"","CarrierName":"PostNL","CountryOfManufacturer":"CN","Base64String":""],"NumberOfSku":1,"NumberOfUnits":1}'  "http://server.com/Magicxpi4.6/MgWebRequester.dll?appname=IFSCarolina_Prod&prgname=HTTP&arguments=-AREST_Incoming%%23IncomingFile"

,收到的JSON如下:

{TransmissionID:SO000001,CustomerSO:SO000001,EndUserName:Roi_Test,Hold:,RequestedDate:2019-02-24 15:00,Currency:EUR,Address1:Calle del Maestro Bagant,Address2:,BuildingName:,BuildingNumber:1,Floor:1,ContactPerson:roi,City:Valencia,CountryAbbriviation:ES,Email:aaa@joomi.co.il,Phone:050-7680249,Zip:46015,Remark:,Incoterm:DDP,Status:,Item:[OrderLine:1,ItemName:cl111,ItemDescription:Description,Quantity:1.,PriceCurrency:EUR,Price:219.9,HSCode:9900000003463,AWBNumber:,CarrierName:PostNL,CountryOfManufacturer:CN,Base64String:],NumberOfSku:1,NumberOfUnits:1}

收到的数据看起来像不是JSON的字符串,字段和值缺少“ ...”。

我已经尝试在单引号/双引号之间进行切换,但是它不起作用。

关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

您正在发送损坏的json,具体是

"Item": [
    "OrderLine": 1,
    "ItemName": "cl111",
    "ItemDescription": "Description",
    "Quantity": "1.",
    "PriceCurrency": "EUR",
    "Price": "219.9",
    "HSCode": "9900000003463",
    "AWBNumber": "",
    "CarrierName": "PostNL",
    "CountryOfManufacturer": "CN",
    "Base64String": ""
],

无效的JSON。在PHP中,这将是一个合法的数组,因为PHP允许在数组中使用字符串键,而JSON(和JavaScript)则不允许。但是在JSON中,对象可以具有字符串键,因此,与合法json最接近的事情是使"Item"成为对象而不是数组,例如,这将是合法JSON:

{
    "TransmissionID": "SO000001",
    "CustomerSO": "SO000001",
    "EndUserName": "Roi_Test",
    "Hold": "",
    "RequestedDate": "2019-02-24 15:00",
    "Currency": "EUR",
    "Address1": "Calle del Maestro Bagant",
    "Address2": "",
    "BuildingName": "",
    "BuildingNumber": "1",
    "Floor": "1",
    "ContactPerson": "roi",
    "City": "Valencia",
    "CountryAbbriviation": "ES",
    "Email": "aaa@joomi.co.il",
    "Phone": "050-7680249",
    "Zip": "46015",
    "Remark": "",
    "Incoterm": "DDP",
    "Status": "",
    "Item": {
        "OrderLine": 1,
        "ItemName": "cl111",
        "ItemDescription": "Description",
        "Quantity": "1.",
        "PriceCurrency": "EUR",
        "Price": "219.9",
        "HSCode": "9900000003463",
        "AWBNumber": "",
        "CarrierName": "PostNL",
        "CountryOfManufacturer": "CN",
        "Base64String": ""
    },
    "NumberOfSku": 1,
    "NumberOfUnits": 1
}

顺便说一句,您是手工制作这么大的jsons复杂的jsons吗?我认为您应该改用脚本语言,以使其更具可读性和可维护性,例如,这是使用PHP-cli的方法:

#!/usr/bin/env php
<?php
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => 'http://server.com/Magicxpi4.6/MgWebRequester.dll?appname=IFSCarolina_Prod&prgname=HTTP&arguments=-AREST_Incoming%%23IncomingFile',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json'
    ) ,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => json_encode(array(
        'TransmissionID' => 'SO000001',
        'CustomerSO' => 'SO000001',
        'EndUserName' => 'Roi_Test',
        'Hold' => '',
        'RequestedDate' => '2019-02-24 15:00',
        'Currency' => 'EUR',
        'Address1' => 'Calle del Maestro Bagant',
        'Address2' => '',
        'BuildingName' => '',
        'BuildingNumber' => 1,
        'Floor' => 1,
        'ContactPerson' => 'roi',
        'City' => 'Valencia',
        'CountryAbbriviation' => 'ES',
        'Email' => 'aaa@joomi.co.il',
        'Phone' => '050-7680249',
        'Zip' => '46015',
        'Remark' => '',
        'Incoterm' => 'DDP',
        'Status' => '',
        'Item' => array(
            'OrderLine' => 1,
            'ItemName' => 'cl111',
            'ItemDescription' => 'Description',
            'Quantity' => '1.',
            'PriceCurrency' => 'EUR',
            'Price' => 219.9,
            'HSCode' => '9900000003463',
            'AWBNumber' => '',
            'CarrierName' => 'PostNL',
            'CountryOfManufacturer' => 'CN',
            'Base64String' => '',
        ) ,
        'NumberOfSku' => 1,
        'NumberOfUnits' => 1,
    )) ,
));
curl_exec($ch);
curl_close($ch);
  • 最后2件事,JSON中的Item的“数量”为1.-应该是圆点,还是错字?
  • 我很长一段时间都没有接触过PayPal REST api,但这让我想起了PayPal REST api,在该API中,我认为Item应该是对象数组,而不仅仅是对象,如果您想要的就是"Item":[{...}](在JSON中)或'Item' => array(array(...))(在PHP中)