Reputation:
I need to call webservice api url & fetch result....
JSON payload provided by API team :
{
"request_id": "R09",
"client_name": "CLIENT",
"services": [
{
"service_code": "REGULAR",
"service_details": [
{
"service_leg": "FORWARD",
"service_data": {
"vendor_name":"Ekart",
"amount_to_collect": 1000,
"dispatch_date": "",
"customer_promise_date": "",
"delivery_type": "SMALL",
"source": {
"address": {
"first_name": "AVThamizhmahan",
"address_line1": "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2": "Tamil Nadu",
"pincode": "400066",
"city": "Villupuram",
"state": "Tamil Nadu",
"primary_contact_number": "88888888"
}
},
"destination": {
"address": {
"first_name": "AVThamizhmahan",
"address_line1": "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2": "Tamil Nadu",
"pincode": "400066",
"city": "Villupuram",
"state": "Tamil Nadu",
"primary_contact_number": "88888888"
}
},
"return_location": {
"address": {
"first_name": "AVThamizhmahan",
"address_line1": "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2": "Tamil Nadu",
"pincode": "400066",
"city": "Villupuram",
"state": "Tamil Nadu",
"primary_contact_number": "88888888"
}
}
},
"shipment": {
"client_reference_id": "ABCD",
"tracking_id": "CLTC0000000001",
"shipment_value": 2000,
"shipment_dimensions": {
"length": {
"value": 1
},
"breadth": {
"value": 1
},
"height": {
"value": 1
},
"weight": {
"value": 1
}
},
"return_label_desc_1": "",
"return_label_desc_2": "",
"shipment_items": [
{
"product_id": "134883075",
"item_content":{},
"category": "Apparel",
"product_title": "Mast&HarbourBlackHandbag: Handbags",
"quantity": 1,
"cost": {
"total_sale_value": 500,
"total_tax_value": 100 ,
“tax_breakup”:{
“cgst”:0.0,
“sgst”:0.0,
“igst”:0.0
}
},
"seller_details": {
"seller_reg_name": "Seller Registered Legal Name",
"vat_id": "",
"cst_id": "",
“tin_id”:””,
“gstin_id”:””
},
“hsn” :””,
“ern”:””,
“discount”:0.0,
"legal_entity": "",
"billable_entity": "",
"cod_remittance_entity": "",
"item_attributes": [
{
"name": "order_id",
"value": "ABCD"
},
{
"name": "invoice_id",
"value": "INV1234"
},
{
"name": "item_dimensions",
"value": "l:b:h:w"
},
{
"name": "brand_name",
"value": "Peter England"
}
],
"handling_attributes": [
{
"name": "isFragile",
"value": "false"
},
{
"name": "isDangerous",
"value": "false"
}
]
}
]
}
}
]
}
]
}
I am calling JSon as array & passing url :
<?php
$url = "https://staging.logistics.com/v2/shipments/create";
$data = array
(
"request_id"=> "R09",
"client_name"=> "DOC",
"services"=> [
array (
"service_code"=> "REGULAR",
"service_details"=> [
array (
"service_leg"=> "FORWARD",
"service_data"=> array (
"vendor_name"=>"Ekart",
"amount_to_collect"=> 1000,
"dispatch_date"=> "",
"customer_promise_date"=> "",
"delivery_type"=> "SMALL",
"source"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "400066",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"destination"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "400066",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"return_location"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "400066",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
)
),
"shipment"=> array (
"client_reference_id"=> "ABCD",
"tracking_id"=> "DOCC0000000001",
"shipment_value"=> 2000,
"shipment_dimensions"=> array (
"length"=> array (
"value"=> 1
),
"breadth"=> array (
"value"=> 1
),
"height"=> array (
"value"=> 1
),
"weight"=> array (
"value"=> 1
)
),
"return_label_desc_1"=> "",
"return_label_desc_2"=> "",
"shipment_items"=> [
array (
"product_id"=> "134883075",
"item_content"=>array (),
"category"=> "Apparel",
"product_title"=> "Mast&HarbourBlackHandbag=> Handbags",
"quantity"=> 1,
"cost"=> array (
"total_sale_value"=> 500,
"total_tax_value"=> 100 ,
"tax_breakup"=>array (
"cgst"=>0.0,
"sgst"=>0.0,
"igst"=>0.0
)
),
"seller_details"=> array (
"seller_reg_name"=> "Seller Registered Legal Name",
"vat_id"=> "",
"cst_id"=> "",
"tin_id"=>"",
"gstin_id"=>""
),
"hsn" =>"",
"ern"=>"",
"discount"=>0.0,
"legal_entity"=> "",
"billable_entity"=> "",
"cod_remittance_entity"=> "",
"item_attributes"=> [
array (
"name"=> "order_id",
"value"=> "ABCD"
),
array (
"name"=> "invoice_id",
"value"=> "INV1234"
),
array (
"name"=> "item_dimensions",
"value"=> "l=>b=>h=>w"
),
array (
"name"=> "brand_name",
"value"=> "Peter England"
)
],
"handling_attributes"=> [
array (
"name"=> "isFragile",
"value"=> "false"
),
array (
"name"=> "isDangerous",
"value"=> "false"
)
]
)
]
)
)
]
)
]
);
echo $data=json_encode($data);
$data_string = json_encode(array(
"Payload" => $data
));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type : application/json"
));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization : Basic ZGFpbHlvcmRlcnM6ZHVtbf4fsww="
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_POST, 1);
curl_close($ch);
echo "$result";
?>
Response :
400 Bad request
Your browser sent an invalid request.
Am i sending in proper format ?
Here is APi Document : https://drive.google.com/open?id=13AstBwus-uErvElg2-FN0knIy1m9lMHa
Please let me know if you need any other information....
I am using ekart api.....
Thanks in Advance....
Update
Below is POSTMAN result :
Update 2 :
Error :
{"response":[{"status":"REQUEST_REJECTED","message":["X_Merchant_Code is null "]}],"request_id":"2aa4cf98-0b59-4e2d-ba10-79c39663d6b6"}
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
$url = "https://staging.ekartlogistics.com/v2/shipments/create";
$data1 = array
(
"request_id"=> "835",
"client_name"=> "DOC",
"services"=> [
array (
"service_code"=> "REGULAR",
"service_details"=> [
array (
"service_leg"=> "FORWARD",
"service_data"=> array (
"vendor_name"=>"Ekart",
"amount_to_collect"=> 1000,
"dispatch_date"=> "",
"customer_promise_date"=> "",
"delivery_type"=> "SMALL",
"source"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "560034",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"destination"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "110037",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
),
"return_location"=> array (
"address"=> array (
"first_name"=> "AVThamizhmahan",
"address_line1"=> "31675 ES Garden Vazhudaretty Post Villupuram",
"address_line2"=> "Tamil Nadu",
"pincode"=> "110037",
"city"=> "Villupuram",
"state"=> "Tamil Nadu",
"primary_contact_number"=> "88888888"
)
)
),
"shipment"=> array (
"client_reference_id"=> "ABCD",
"tracking_id"=> "DOCC0000000001",
"shipment_value"=> 2000,
"shipment_dimensions"=> array (
"length"=> array (
"value"=> 1
),
"breadth"=> array (
"value"=> 1
),
"height"=> array (
"value"=> 1
),
"weight"=> array (
"value"=> 1
)
),
"return_label_desc_1"=> "",
"return_label_desc_2"=> "",
"shipment_items"=> [
array (
"product_id"=> "134883075",
"item_content"=>array (),
"category"=> "Apparel",
"product_title"=> "Mast&HarbourBlackHandbag=> Handbags",
"quantity"=> 1,
"cost"=> array (
"total_sale_value"=> 500,
"total_tax_value"=> 100 ,
"tax_breakup"=>array (
"cgst"=>0.0,
"sgst"=>0.0,
"igst"=>0.0
)
),
"seller_details"=> array (
"seller_reg_name"=> "Seller Registered Legal Name",
"vat_id"=> "",
"cst_id"=> "",
"tin_id"=>"",
"gstin_id"=>""
),
"hsn" =>"",
"ern"=>"",
"discount"=>0.0,
"legal_entity"=> "",
"billable_entity"=> "",
"cod_remittance_entity"=> "",
"item_attributes"=> [
array (
"name"=> "order_id",
"value"=> "ABCD"
),
array (
"name"=> "invoice_id",
"value"=> "INV1234"
),
array (
"name"=> "item_dimensions",
"value"=> "l=>b=>h=>w"
),
array (
"name"=> "brand_name",
"value"=> "Peter England"
)
],
"handling_attributes"=> [
array (
"name"=> "isFragile",
"value"=> "false"
),
array (
"name"=> "isDangerous",
"value"=> "false"
)
]
)
]
)
)
]
)
]
);
echo $data=json_encode(array("payload"=>$data1));
echo "<br/><br/>";//die;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/json", "X_Merchant_Code: DOC"));
curl_setopt($curl, CURLOPT_USERPWD, "dailyorders:dummyKey"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
$curl_response = curl_exec($curl);
curl_close($curl);
print_r($curl_response);die;
?>
Upvotes: 1
Views: 3165
Reputation: 15376
When setting the headers you have a space between the header name and the separating colon. That results in Curl using two headers, a Content-Type : application/json
header and a Content-Type: application/x-www-form-urlencoded
header, which is the default when posting data. The server could identify the content type as "x-www-form-urlencoded" using the second header, however the first header would cause a 400 response, because there should be no space between the header and the colon. From rfc7230, section 3.2.4:
No whitespace is allowed between the header field-name and colon. In the past, differences in the handling of such whitespace have led to security vulnerabilities in request routing and response handling. A server MUST reject any received request message that contains whitespace between a header field-name and colon with a response code of 400 (Bad Request).
<!- -!>
Your code should return successful response using the headers below.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbf4fsww=",
"Content-Type: application/json"
));
When debugging such issues, it's best to enable the verbose mode which prints helpful information about the connection.
curl_setopt($ch, CURLOPT_VERBOSE, true);
In your Postman request you're submitting a HTTP_X_MERCHANT_CODE
header; you should include that in your Curl request.
$headers = array(
"Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbXlLZXk=",
"Content-Type: application/json",
"HTTP_X_MERCHANT_CODE: DOC",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
This would fix the "X_Merchant_Code is null" error, but the server would return a new error message: "Expected BEGIN_OBJECT but was BEGIN_ARRAY", pointing at: ...shipment.shipment_items[0].item_content
, because the json data does not have the required format. The item_content
key is an empty array, which gets encoded as []
, but the server expects a json object ({}
). We can fix this issue if we use a null object.
$data['services'][0]['service_details'][0]['shipment']['shipment_items'][0]['item_content'] = (object)null;
The complete code:
<?php
$data = [
"request_id" => "835",
"client_name" => "DOC",
"services" => [
[
"service_code" => "REGULAR",
"service_details" => [
[
"service_leg" => "FORWARD",
"service_data" => [
"vendor_name" => "Ekart",
"amount_to_collect" => 1000,
"dispatch_date" => "",
"customer_promise_date" => "",
"delivery_type" => "SMALL",
"source" => [
"address" => [
"first_name" => "AVThamizhmahan",
"address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2" => "Tamil Nadu",
"pincode" => "400066",
"city" => "Villupuram",
"state" => "Tamil Nadu",
"primary_contact_number" => "88888888"
]
],
"destination" => [
"address" => [
"first_name" => "AVThamizhmahan",
"address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2" => "Tamil Nadu",
"pincode" => "400066",
"city" => "Villupuram",
"state" => "Tamil Nadu",
"primary_contact_number" => "88888888"
]
],
"return_location" => [
"address" => [
"first_name" => "AVThamizhmahan",
"address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2" => "Tamil Nadu",
"pincode" => "400066",
"city" => "Villupuram",
"state" => "Tamil Nadu",
"primary_contact_number" => "88888888"
]
]
],
"shipment" => [
"client_reference_id" => "ABCD",
"tracking_id" => "DOCC0000000001",
"shipment_value" => 2000,
"shipment_dimensions" => [
"length" => [
"value" => 1
],
"breadth" => [
"value" => 1
],
"height" => [
"value" => 1
],
"weight" => [
"value" => 1
]
],
"return_label_desc_1" => "",
"return_label_desc_2" => "",
"shipment_items" => [
[
"product_id" => "134883075",
"item_content" => (object)null,
"category" => "Apparel",
"product_title" => "Mast&HarbourBlackHandbag => Handbags",
"quantity" => 1,
"cost" => [
"total_sale_value" => 500,
"total_tax_value" => 100
],
"seller_details" => [
"seller_reg_name" => "Seller Registered Legal Name",
"vat_id" => "",
"cst_id" => ""
],
"legal_entity" => "",
"billable_entity" => "",
"cod_remittance_entity" => "",
"item_attributes" => [
[
"name" => "order_id",
"value" => "ABCD"
],
[
"name" => "invoice_id",
"value" => "INV1234"
],
[
"name" => "item_dimensions",
"value" => "l:b:h:w"
],
[
"name" => "brand_name",
"value" => "Peter England"
]
],
"handling_attributes" => [
[
"name" => "isFragile",
"value" => "false"
],
[
"name" => "isDangerous",
"value" => "false"
]
]
]
]
]
]
]
]
]
];
$url = "https://staging.ekartlogistics.com/v2/shipments/create";
$data = json_encode($data);
$headers = array(
"Content-Type: application/json",
"HTTP_X_MERCHANT_CODE: DOC",
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "dailyorders:dummyKey"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
curl_setopt($curl, CURLOPT_VERBOSE, true);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";
?>
Upvotes: 1
Reputation:
Passing Merchant key in proper form solved the issue....
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://staging.ekartlogistics.com/v2/shipments/create",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\n \"request_id\": 835,\n \"client_name\": \"DOC\",\n \"services\": [\n {\n \"service_code\": \"REGULAR\",\n \"service_details\": [\n {\n \"service_leg\": \"FORWARD\",\n \"service_data\": {\n \"vendor_name\": \"Ekart\",\n \"amount_to_collect\": \"5382.00\",\n \"delivery_type\": \"SMALL\",\n \"source\": {\n \"address\": {\n \"first_name\": \"Ebay\",\n \"address_line1\": \"sqsq\",\n \"address_line2\": \"gvhvh\",\n \"pincode\": \"110037\",\n \"city\": \"gvhbj\",\n \"state\": \"Delhi\",\n \"primary_contact_number\": \"9987654321\"\n }\n },\n \"destination\": {\n \"address\": {\n \"first_name\": \"Dgwdh\",\n \"address_line1\": \"C-238\",\n \"address_line2\": \"Nagar, Amrut Nagar, Ghatkopar (W)\",\n \"pincode\": \"560034\",\n \"city\": \"Delhi\",\n \"state\": \"South West Delhi\",\n \"primary_contact_number\": \"9716632816\"\n }\n },\n \"return_location\": {\n \"address\": {\n \"first_name\": \"Primasource Products and \",\n \"address_line1\": \"No 99 4th Cross , Prithvi Layout\",\n \"address_line2\": \"ECC Raod\",\n \"pincode\": \"560034\",\n \"city\": \"Bangalore\",\n \"state\": \"Karnataka\",\n \"primary_contact_number\": \"9987654321\"\n }\n }\n },\n \"shipment\": {\n \"tracking_id\": \"DOCC0000000837\",\n \"shipment_value\": 2000,\n \"shipment_dimensions\": {\n \"length\": {\n \"value\": \"12\"\n },\n \"breadth\": {\n \"value\": \"12\"\n },\n \"height\": {\n \"value\": \"12\"\n },\n \"weight\": {\n \"value\": \"1\"\n }\n },\n \"return_label_desc_1\": \"\",\n \"return_label_desc_2\": \"\",\n \"shipment_items\": [\n {\n \"product_id\": \"895\",\n \"product_title\": \"dwdwd\",\n \"quantity\": 23,\n \"cost\": {\n \"total_sale_value\": \"5382.00\",\n \"total_tax_value\": 5382,\n \"tax_breakup\": {\n \"cgst\": 0,\n \"sgst\": 0,\n \"igst\": 0\n }\n },\n \"seller_details\": {\n \"seller_reg_name\": \"Ebay\",\n \"vat_id\": \"\",\n \"cst_id\": \"\",\n \"tin_id\": \"\",\n \"gstin_id\": null\n },\n \"hsn\": \"123456\",\n \"ern\": \"\",\n \"discount\": \"0.00\",\n \"item_attributes\": [\n {\n \"name\": \"order_id\",\n \"value\": 835\n },\n {\n \"name\": \"invoice_id\",\n \"value\": \"NA\"\n },\n {\n \"name\": \"item_dimensions\",\n \"value\": \":\"\n },\n {\n \"name\": \"brand_name\",\n \"value\": null\n }\n ]\n }\n ]\n }\n }\n ]\n }\n ]\n}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"HTTP_X_MERCHANT_CODE: DOC",
"Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbXlLZXk=",
"Postman-Token: bf0fd969-0618-432a-a222-d4a67bff6cd2",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Upvotes: 1
Reputation: 526
assuming that your params are correct.
try:
echo $data=json_encode($data);
//$data_string = json_encode(array(
// "Payload" => $data
//));
$username = "";
$password = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// "Authorization : yourtoken",
//));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type : application/json"
));
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);//since you are using basic auth
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close ($ch);
Upvotes: 0
Reputation: 555
It looks like in your JSON array you have an error :
“vendor_name”:”Ekart”,
Is not quoted correctly :
"vendor_name":"Ekart",
Should work better.
There are a few more errors like this. You should use a JSON editor to check your JSON array.
Upvotes: 0