Search code examples
javascriptapipostmanmultipartform-dataimgur

POSTMAN PUT request not updating values


Assuming the position of the API tester of https://imgur.com/ , Im testing the PUT request to change account settings. I am following this api doc enter image description here Link for above https://apidocs.imgur.com/#7bc88d39-d06d-4661-afff-38ea5b9a1d0a

Steps to check this

  1. Add the relevant info as below, I am setting show_mature and newsletter_subscribed to true

enter image description here 2. Set the Access token enter image description here 3. Click on send the response for this is 200 as shown below enter image description here

  1. Check if the details have updated as shown in the following screenshot enter image description here

Expected: To have show_mature and newsletter_subscribed values set to true Actual: show_mature and newsletter_subscribed values are false

Would be really appreciated if someone could let me know why this is happening? Thanks


Solution

  • From the Imgur API docs...

    Need help?

    The Imgur engineers are always around answering questions. The quickest way to get help is by posting your question on StackOverflow with the Imgur tag.

    Real helpful Imgur 🙄.

    Answering here to provide a canonical answer in the tag for this nonsense.

    All the API examples in the documentation use some form of multipart/form-data request body payloads. Eg

    var myHeaders = new Headers();
    myHeaders.append("Authorization", "Bearer {{accessToken}}");
    
    var formdata = new FormData();
    
    var requestOptions = {
      method: 'PUT',
      headers: myHeaders,
      body: formdata,
      redirect: 'follow'
    };
    
    fetch("https://api.imgur.com/3/account/{{username}}/settings", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));
    

    and

    curl --location --request POST 'https://api.imgur.com/oauth2/token' \
    --form 'refresh_token="{{refreshToken}}"' \
    --form 'client_id="{{clientId}}"' \
    --form 'client_secret="{{clientSecret}}"' \
    --form 'grant_type="refresh_token"'
    

    With the exception of any upload related endpoints, this is ABSOLUTELY INCORRECT. Passing data as multipart/form-data requires the API to handle that request content-type and guess what, the Imgur API does not.

    What they do accept is application/x-www-form-urlencoded.

    • In Postman that's the x-www-form-urlencoded option, not form-data
    • In cURL that's the -d option, not --form
    • In JavaScript that's URLSearchParams, not FormData