Roi Solberg
Roi Solberg

Reputation: 3

Invoking endpoint with CURL- Post method and json is corrupt

I am invoking an endpoint of mine with CURL with the following command:

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":"[email protected]","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"

and the JSON received is as follow:

{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:[email protected],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}

The data received is looking like a string not like a JSON, the fields and values are missing the " signs...

I have already tried to change between single/double quotes and it did not work.

Any ideas on how to resolve this?

Upvotes: 0

Views: 241

Answers (1)

hanshenrik
hanshenrik

Reputation: 21463

you ARE sending a corrupt json, specifically

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

is not valid JSON. in PHP this would be a legal array, as PHP allows string-keys in arrays, but JSON (and JavaScript) does not. but in JSON, objects can have string keys, so the closest thing you'll get to a legal json would be to make "Item" an object instead of an array, for example this would be legal 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": "[email protected]",
    "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
}

btw, are you hand-crafting such large jsons complex jsons? i think you should switch to a scripting language instead to make it more readable and maintainable, ... for example, here is how to do it with 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' => '[email protected]',
        '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 last things, the "Quantity" of the Item in your JSON is 1. - is the dot supposed to be there, or is it a typo?
  • i haven't touched the PayPal REST api in a long time, but this reminds me of the PayPal REST api, and in that API, i believe Item is supposed to be an array-of-objects, not just an object, if that's what you want then it would be "Item":[{...}] (in JSON) or 'Item' => array(array(...)) (in PHP)

Upvotes: 1

Related Questions