Question

How to populate HTTPCaller body from a JSON file


When trying to call an API using HTTPCaller (POST) "upload from file" option, FME fails with error code 500.

However using exactly the same piece of JSON structure as "specify upload body", then the HTTPCaller works fine.

So how is it possible, to change the upload body for each time I read a new JSON record from the file.

Any ideas to make this work, one way or another, are very welcome.


10 replies

Userlevel 6
Badge +33

More possible solutions. You could use a FeatureReader to read and merge the JSON as attribute.

httpcallerjsonbody.fmwt

Thanks a lot, nielsgerrits. I have copied your solution completely. I got the idea, but unfortunately it still does not work. The FeatureReader splits my text up in 23 items, showing the exact JSON structure. However, the JSONValidator rejects them all, saying the JSON data is incomplete and ended unexpected.

But running the same JSON text as a fixed "body" works fine. Could it be, that [ ] square brackets are not allowed in the text_line_data?

Userlevel 6
Badge +33

Thanks a lot, nielsgerrits. I have copied your solution completely. I got the idea, but unfortunately it still does not work. The FeatureReader splits my text up in 23 items, showing the exact JSON structure. However, the JSONValidator rejects them all, saying the JSON data is incomplete and ended unexpected.

But running the same JSON text as a fixed "body" works fine. Could it be, that [ ] square brackets are not allowed in the text_line_data?

@fritspoulsen12 You need to read the json as text, and the whole file in one attribute:

@nielsgerrits. I have used exact same parameters, as yours, but still I get my text_line_data split in many items.

Userlevel 6
Badge +33

@nielsgerrits. I have used exact same parameters, as yours, but still I get my text_line_data split in many items.

Hit that parameters button :) and put "Read Whole File at Once" to "Yes".

@nielsgerrits. OMG - sorry, didn't see that. Now it's just one text_line_data.

However, not surpricingly I end up with the same error, as when I originally used the "upload from file" option in the HTTPCaller.

Attribute(encoded: UTF-8) : `_response_body' has value `{"message":"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Domain.Dto.Ansoegninger.MaalerAnsoegningDto' because the type requires a JSON object (e.g. {\\"name\\":\\"value\\"}) to deserialize correctly.\\r\\nTo fix this error either change the JSON to a JSON object (e.g. {\\"name\\":\\"value\\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\\r\\nPath ''."}'

 

So I can't use this method as an alternative anyway. But nice to know this method too. Thanks nielserrits.

Userlevel 6
Badge +33

@nielsgerrits. OMG - sorry, didn't see that. Now it's just one text_line_data. 

However, not surpricingly I end up with the same error, as when I originally used the "upload from file" option in the HTTPCaller.  

Attribute(encoded: UTF-8)         : `_response_body' has value `{"message":"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Domain.Dto.Ansoegninger.MaalerAnsoegningDto' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath ''."}'

 

So I can't use this method as an alternative anyway. But nice to know this method too. Thanks nielserrits. 

No worries mate, it is always easier when you know where to look at :)

Looks like the problem is in you JSON structure tho. Can you post it here?

If I interpret the response right you might need this structure for your list items:

    {
      "name": "THEMES",
      "value": [
        "airports",
        "cenart"
      ]
    }

@nielsgerrits You're right - learn by doing (and trying) is the way ahead.

My problem is, that this JSON date structure, that FME's complaining about, is not known at all in the API.

Domain.Dto.Ansoegninger.MaalerAnsoegningDto

As said earlier my JSON works fine as a HTTPCaller body constant or calling the API from outside FME ie. POSTMAN.

 

OMG - just discovered now when writing this, that the problem is caused by the [ ] brackets before and after the real JSON stuff. They are only in the file - see below.

If I can remove them, I'm sure, it will work now. Thanks again for your advice and patience.

[

{

"json_featuretype" : "NewFeatureType",

"antalMaalere" : "1",

"maalerBemaerkning" : "VVS bemærkning (fra blanket):Stik placering vides ikke endnu, hvad er der mulighed for efter hvor jeres ledninger ligger.\\r\\n",

"dimensionsgivendeVandstroem" : "0",

"maalertype" : {

"id" : 1,

"description" : "Blivende vandstik og blivende måler"

},

"maalerplacering" : {

"id" : 1,

"description" : "Bryggers"

},

"maalerdimension" : {

"id" : 2,

"description" : "Q3=4 (Qn2,5)"

},

"bemaerkning" : "2965_MIGR",

"placering" : {

"wkt" : "POINT(0 0)/"

},

"anvendelse" : {

"id" : 5,

"description" : "Andet"

}

}

]

Userlevel 6
Badge +33

@nielsgerrits You're right - learn by doing (and trying) is the way ahead.

My problem is, that this JSON date structure, that FME's complaining about, is not known at all in the API.

Domain.Dto.Ansoegninger.MaalerAnsoegningDto

As said earlier my JSON works fine as a HTTPCaller body constant or calling the API from outside FME ie. POSTMAN.

 

OMG - just discovered now when writing this, that the problem is caused by the [ ] brackets before and after the real JSON stuff. They are only in the file - see below.

If I can remove them, I'm sure, it will work now. Thanks again for your advice and patience.

[

{

"json_featuretype" : "NewFeatureType",

"antalMaalere" : "1",

"maalerBemaerkning" : "VVS bemærkning (fra blanket):Stik placering vides ikke endnu, hvad er der mulighed for efter hvor jeres ledninger ligger.\\r\\n",

"dimensionsgivendeVandstroem" : "0",

"maalertype" : {

"id" : 1,

"description" : "Blivende vandstik og blivende måler"

},

"maalerplacering" : {

"id" : 1,

"description" : "Bryggers"

},

"maalerdimension" : {

"id" : 2,

"description" : "Q3=4 (Qn2,5)"

},

"bemaerkning" : "2965_MIGR",

"placering" : {

"wkt" : "POINT(0 0)/"

},

"anvendelse" : {

"id" : 5,

"description" : "Andet"

}

}

]

I think the response is returned by the API you are requesting to, not by FME.

@nielsgerrits Yes, you're right. I get the same error when POSTing from POSTMAN with the [ ] brackets.

Reply