Search code examples
vbadecodehttp-status-code-400vin

Weird limit on NHTSA batch VIN decode via WinHTTPRequest


I'm encountering a problem (HTTP STATUS 400 Bad Request) using code to decode VINs if I send more than 12, but if I send a list of over 100 by pasting into website, I get results.

I'm using following code to send a list of VINs to get back information from NHTSA vin decoder (https://vpic.nhtsa.dot.gov/api/, Decode VIN (flat format) in a Batch)

request = "VIN1;VIN2;VIN3; ...?format=xml"  <- just example to show format of data

Set http = New WinHTTP.WinHttpRequest
http.settimeouts 30000,30000,30000,30000
http.open "POST","https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValuesBatch", False
http.SetRequestHeader "Content-Type", "text/plain"
http.send request

response = http.ResponseText 

I thought this might be some length limitation, so I've tried setting content length to the length of the string being sent, but that didn't seem to help. I've searched quite a bit, but couldn't find anything in particular that would explain. I thought maybe NHTSA has a limit on what can be sent back, but since I can paste the list into the website manually and get the results that didn't make a lot of sense. It provides the same XML formatted data back that way. I'm not sure what else to try, so hoping someone else will have other ideas. I'm new to using WinHTTPRequest, so it is very possible I'm making a really dumb mistake.


Solution

  • Your request is not valid, because your VINXvalues don't have a key (DATA).

    You can see this by using developer tools on your browser when testing your example link. In Firefox (others are similar) you need the Net->Log turned on in Consoleof dev tools, then you can see the data send on POST

    Rawdata:
    DATA=5UXWX7C5*BA%2C2011%3B+5YJSA3DS*EF+&format=XML
    

    According to this your request string should be:

    request = "DATA=VIN1;VIN2;VIN3; ...&format=xml" ' Post array elements are separated by &
    

    Your content type:

    http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"