Last active
August 29, 2015 14:18
-
-
Save stim371/325013e5aa2a9a8a75fd to your computer and use it in GitHub Desktop.
Different content types cause the message body to split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'http://api.app.dev:3000/messages'; | |
$data = array( | |
'data' => | |
json_encode(array( | |
'test' => 'hello & more details', | |
'fail' => 'asdf;sadfsfa=' | |
)) | |
); | |
$options = array( | |
'http' => array( | |
// combining this with the params that are only partially JSON causes a 500 error | |
// This makes me think this is not how you are doing it, but I wanted to include it as an example | |
'header' => "Content-type: application/json\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data), | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
// this breaks before it even gets to my code, so I can't see what the params look like | |
// and I'm guessing it's not how you have it set up since those are getting through to my code | |
var_dump($result); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'http://api.app.dev:3000/messages'; | |
$data = array( | |
'data' => array( | |
'test' => 'hello & more details', | |
'fail' => 'asdf;sadfsfa=' | |
) | |
); | |
$options = array( | |
'http' => array( | |
// the content type here or multipart/form-data both cause the body to be split | |
// when the content is put through #json_encode | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => json_encode($data) | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
// server sees {"{\"data\":\"{\\\"test\\\":\\\"hello "=>nil, "more details\\\",\\\"fail\\\":\\\"asdf"=>nil, "sadfsfa"=>"\\\"}\"}"} | |
var_dump($result); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'http://api.app.dev:3000/messages'; | |
$data = array( | |
'data' => | |
// this matches how the data is currently coming through, right? | |
json_encode(array( | |
'test' => 'hello & more details', | |
'fail' => 'asdf;sadfsfa=' | |
)) | |
); | |
$options = array( | |
'http' => array( | |
// setting this seems to keep the JSON from attempting to be parsed until I'm ready | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data), | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
// server sees {"data"=>"{\"test\":\"hello & more details\",\"fail\":\"asdf;sadfsfa=\"}"} | |
var_dump($result); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'http://api.app.dev:3000/messages'; | |
$data = array( | |
'data' => | |
// just setting everything as form attributes also works even though we state that we expect JSON | |
array( | |
'test' => 'hello & more details', | |
'fail' => 'asdf;sadfsfa=' | |
) | |
); | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data), | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
// server sees {"data"=>"{\"test\":\"hello & more details\",\"fail\":\"asdf;sadfsfa=\"}"} | |
var_dump($result); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = 'http://api.app.dev:3000/messages'; | |
$data = array( | |
'data' => array( | |
'test' => 'hello & more details', | |
'fail' => 'asdf;sadfsfa=' | |
) | |
); | |
$options = array( | |
'http' => array( | |
// changing the content-type to json allows all of the parameters to come through cleanly | |
'header' => "Content-type: application/json\r\n", | |
'method' => 'POST', | |
'content' => json_encode($data) | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
// server sees {"data"=>"{\"test\":\"hello & more details\",\"fail\":\"asdf;sadfsfa=\"}"} | |
var_dump($result); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
works.php
example would require a minor code change on my end, since Rails will automatically parse the JSON instead of me trying to do it.works-form-json.php
would not require a code change on my end, I believe.The
works-form-only.php
example would also require a code change but seems to be the cleanest since we don't have to convert back and forth to JSON as an intermediate format.