I have some data I need to post to an endpoint which is a nested array like:
$contentPostData = array(
'contents' => array(
'name' => $comment_text,
'fingerprint' => $fingerprint,
'signers' => array(
$ownerAuthId
)
)
);
When I try:
json_encode($contentPostData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
I get the error:
string(518) "{"timestamp":1578447151168,"message":"Can't read request","details":["JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException
How do I format the contentPostData so that it can be deserialized correctly?
You mention:
json_encode($contentPostData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
But are you assigning the encoded json to $data
? I mean: are you doing:
$data = json_encode($contentPostData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
I see that the webservice to which you are calling uses com.fasterxml.jackson
so most likely expects JSON format and therefore your serialization should be okay (if field names and types match).
I also suggest setting the header to inform that webservice that you are sending a json file (in case it allows other formats):
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
);
So:
$data = json_encode($contentPostData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
If that does not work then most likely: a) your JSON is invalid (different structure is expected) or b) the webservice actually expects a different format than JSON (maybe standard multipart data for example)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.