Forum Discussion
Error in getting POST request in an API
- 6 years ago
You could simply using different code.
This first example you've posted contained wrong brackets.
Why is that?
Could it be that JSON you've been using in Postman has different nesting structure?
Must your -1 be a text instead of a number?
Honestly, I've been there a couple of times to recognize that a simple typo is throwing things off.
Very had to believe that Power Query is the issue here.
Oh sorry, I missed the magic function Json.FromValue: https://www.thebiccountant.com/2018/06/05/easy-post-requests-with-power-bi-and-power-query-using-json-fromvalue/
Source =
Json.Document( Web.Contents(url, Headers=[#"Content-Type"="application/json"],Content = Json.FromValue(Parsed_JSON)]))Hi Sir ImkeF
I will continue to say thank you for always answering my queries 🙂
I tried to change the Source line with the one that you've sent but there is an error in advanced editor:
Source =
Json.Document( Web.Contents(url, Headers=[#"Content-Type"="application/json"],Content = Json.FromValue(Parsed_JSON)]))
I think this is due to the ] in the end.. there should be [ before the word headers, right?
Source =
Json.Document( Web.Contents(url, [Headers=[#"Content-Type"="application/json"],Content = Json.FromValue(Parsed_JSON)]))
But when I did this, 400 error occurs..
DataSource.Error: Web.Contents failed to get contents from '<URL>' (400): Bad Request
Details:
DataSourceKind=Web
DataSourcePath=<URL>
Url=<URL>
- tonmcg6 years agoResolver II
It's far easier to convert your JSON to a Power Query M record and use that as the `Content` value in `Web.Contents`, like so:
let url = URL, bodyContent = [ limit = -1, sort = [ option = "ASC", field = { "FlightDate" }, parameters = {[]}, range = { [ FlightDate = [ from = "02/14/202014:02", to = "02/18/202000:02" ] ] } ] ], binaryContent = Json.FromValue(bodyContent), request = Web.Contents( url, [ Headers = [ #"Content-Type"="application/json" ], Content = binaryContent ] ), jsonResponse = Json.Document(request) in jsonResponseSome advantages:
- you can parameterize values within the record; e.g., the values for the `from` and `to` fields
- you can use native Power Query M `Record` functions to update the record easily
- you don't have to manually parse a JSON string and enclose parts of it with two double quotes ("")
- debugging your code is far easier
To convert JSON to a record in Power Query M, remember that a JSON object is a Power Query M record and a JSON array is a Power Query M list. In other words, {} = [] and [] = {}.
- ImkeF6 years agoCommunity Champion
Hi gilbertendaya ,
now your query has been sent out and you've received an answer from the server. Unfortunately that's all Power Query will tell you about it and you have to use a tool like Fiddler to see more details of what's going wrong.
I'd recommend to check the structure of the nested JSON first, maybe you swapped a list for a record or so.
Otherwise you'd have to compare the traffic that your successful postmal request causes the traffic from Power BI with Fiddler.
- gilbertendaya6 years agoHelper IV
Hi ImkeF
But this is working fine with PostMan 😞
and then here in Power BI desktop, it is not working 😞
- ImkeF6 years agoCommunity Champion
Pretty sure that there is a difference in the code you've used in both versions.
Checking with Fiddler is the way to go forward if you cannot spot it by code analysis.
- gilbertendaya6 years agoHelper IV
Hi ImkeF
What will be the difference with the Fiddler?In Postman, I just use Post method and the URL and then the body.
It throws the data from the database.
Not sure what will be the difference in it.
- ImkeF6 years agoCommunity Champion
You could simply using different code.
This first example you've posted contained wrong brackets.
Why is that?
Could it be that JSON you've been using in Postman has different nesting structure?
Must your -1 be a text instead of a number?
Honestly, I've been there a couple of times to recognize that a simple typo is throwing things off.
Very had to believe that Power Query is the issue here. - gilbertendaya6 years agoHelper IV
Hi ImkeF
It seems the issue was really the way I copy and use the body from PostMan to Power BI Advanced editor.This resolves now my issue.
Thank you very much for the assistance. 🙂