Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Form data in power query seemingly not being sent to API

Hi all   I'm currently working with an API setup by an OEM our company deals with. To work with the API you have to do a POST request with form-data to get an authorization key. This authorization...
  • Mahesh0016's avatar
    3 years ago

    Anonymous 

    let
    // Define the URL for obtaining the authorization token
    authUrl = "YOUR_AUTH_URL",

    // Define the form-data parameters as a record
    formData = [
    key1 = "value1",
    key2 = "value2",
    // Add more form-data fields as needed
    ],

    // Convert the record to a JSON text
    formDataJson = Text.ToBinary(Json.FromValue(formData)),

    // Perform the POST request
    response = Web.Contents(authUrl, [
    Content = formDataJson,
    Headers = [#"Content-Type" = "application/x-www-form-urlencoded"] // Adjust content type if needed
    ]),

    // Parse the response as JSON
    jsonResponse = Json.Document(response),

    // Extract the authorization token from the response
    authToken = jsonResponse[auth_token] // Adjust the field name based on the response structure
    in
    authToken


    In this example, replace "YOUR_AUTH_URL" with the actual URL to obtain the authorization token. Adjust the formData record to include the necessary form-data fields and their corresponding values.

    The error you were encountering might be related to the format of the form-data content. By converting the form-data record to JSON text and specifying the correct content type in the headers, you should be able to perform the POST request successfully.

    Please note that the exact structure and requirements may vary based on the API you're working with, so make sure to refer to the API documentation provided by the OEM for accurate information regarding authentication and API usage.