0

I've been trying to figure out how to make a post request against this kind of json without any luck

URL:/v1.0/country/{countryId}/city/search
Method:POST
Content-Type: application/json
Body: {\"city\":[{\"cityId\":1234, \"min\":0, \"max\":15}]}

Any ideas?

Edit: Here is the code Im working with

<?php

// Auth
$auth = base64_encode("userauthurl:password");
$authContext = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$tokenUrl = 'https://tokenurl.com/token';
$tokenResponse = file_get_contents($tokenUrl, false, $authContext );
$tokenObj = json_decode($tokenResponse);
$access_token = $tokenObj->access_token;

// POST params
$postData = array(
  'body' => '{\"city\":[{\"cityId\":1234, \"min\":0, \"max\":5}]}'
);

// Context POST
$contextGet = stream_context_create([
    "http" => 
    ["method" => "POST", 
    "header" => "Accept: application/json\r\n". 
    "Authorization: Bearer $access_token\r\n",
    "content" => json_encode($postData)
    ]]);


$cityUrl = 'https://urltosendto.com/v1.0/country/{countryId}/city/search';
$cityResponse = file_get_contents($cityUrl, false, $contextGet);
$city = json_decode($cityResponse);
echo json_encode($cityResponse);


?>
  • 5
    What have you tried? Show us some code. – Aleks G Jun 07 '18 at 15:27
  • 1. why do you put `\"` into your JSON where `"` should be? 2. Are you sure you are supposed to POST JSON? The body of a POST request is usually of type application/x-www-form-urlencoded, see also [this question](https://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request). 3. It seems you are calling `json_encode()` on a string that almost looks like JSON. Are you sure this is the correct format? – Karsten Koop Jun 08 '18 at 06:59

0 Answers0