Error 400 bad request

Tags: #<Tag:0x00007f00c5cf3ee8>

Update - As it turns out this was a DA ID10T error. Thank you for the reads and responses

I have a home grown cd database that has worked very well using Freedb.org for accessing CD info. With Freedb closing down I want to modify the app to make requests to th MusicBrainz db for CD info. When I try to submit a query (i.e. https://musicbrainz.org/ws/2/artist?query=artist:queen) I receive the bad request message. If I submit a query (“https://musicbrainz.org/ws/2”) I do receive the document for the Web Service development page… I have set the user agent per the documentation and I am sure I have missed setting something.

The app I am using is written n VB 2010.

TIA

Did you previously use CDDB ID lookups? Or how did you previously look up information on Freedb?

The Rate limiting part of the web service documentation includes information about what we expect to be part of a good User-Agent string. Without seeing your code or any additional information, it is hard to help you further.

3 Likes

Thank you for the response Fresno. I have set the user agent string to Music Manager / 2.0 (email address). When using FreeDB is do use CDDB ID for the lookups. I am not ripping any CDs but trying to update the local DB with CDs that I ripped using Media Player.

To test connecting and downloading from MusicBrainz I copied the code from https://www.winsocketdotnetworkprogramming.com/httpgetrequestdotnetworkprogramming10f.html and created a class. It seems to work fine when connecting to the https://musicbriainz.org and I receive the web page.

Below is the code I am using to test.

Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Web
Public Class HttpPostRequest
’ Displays simple usage information.
Shared Sub usage()
Console.WriteLine(“Executable_file_name [-u URL] [-d data] [-s file] [-p proxy]”)
Console.WriteLine(“Available options:”)
Console.WriteLine(" -u URL URL to post data to")
Console.WriteLine(" -d data Data to post")
Console.WriteLine(" -s file File name to save response to")
Console.WriteLine(" -p HTTP URL Proxy to use for post operation")
Console.WriteLine()
End Sub
’ This routine validates the data being posted to the web page. It parses
’ the string for reserved characters ‘?’, ‘=’, and ‘&’. The individual
’ validated parts are returned via a StringBuilder object.
’ Data to validate
’ StringBuilder object representing the parsed elements
Public Function ValidatePostData(ByVal postData As String) As StringBuilder
Dim encodedPostData As StringBuilder = New StringBuilder()
’ These characters should be more out there…
Dim reservedChars() As Char = {"?", “=”, “&”, “+”}
Dim pos As Integer
Dim offset As Integer
’ Validate the data to be posted
Console.WriteLine(“Validating the data to be posted…”)
offset = 0
While (offset < postData.Length)
pos = postData.IndexOfAny(reservedChars, offset)
If (pos = -1) Then
’ Append the remaining part of the string
Console.WriteLine(“Appending the remaining part of the string…”)
encodedPostData.Append(HttpUtility.UrlEncode(postData.Substring(offset, postData.Length - offset)))
Exit While
End If
’ Found a special character so append up to the special character
Console.WriteLine(“Found a special character so append up to the special character…”)
encodedPostData.Append(HttpUtility.UrlEncode(postData.Substring(offset, pos - offset)))
encodedPostData.Append(postData.Substring(pos, 1))
offset = pos + 1
End While
ValidatePostData = encodedPostData
End Function
’ This method creates an HttpWebRequest object, sets the method to “POST”,
’ and builds the data to post. Once the HttpWebRequest object is created,
’ the request stream is obtained and the post data is sent and the
’ request stream closed. The response is then retrieved.
’ URL to post data to
’ Data to post
’ Proxy server to use
’ Filename to save response to
Public Sub HttpMethodPost(ByVal postUrl As String, ByVal postData As String, ByVal proxyServer As IWebProxy, ByVal saveFile As String)
Dim httpRequest As HttpWebRequest = Nothing
Dim httpResponse As HttpWebResponse = Nothing
Dim httpPostStream As Stream = Nothing
Dim httpResponseStream As BinaryReader = Nothing
Dim localFile As FileStream = Nothing
saveFile = “q:\httptest.txt”
Try
Dim encodedPostData As StringBuilder
Dim postBytes() As Byte = Nothing
’ Create HTTP web request
Console.WriteLine(“Creating HTTP web request…”)
httpRequest = CType(WebRequest.Create(postUrl), HttpWebRequest)
'httpRequest.UserAgent = “Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0”
httpRequest.UserAgent = “Music Manager/1.0 (p*********5@yahoo.com)”
’ Change method from the default “GET” to “POST”
Console.WriteLine(“Changing method from the default GET to POST…”)
httpRequest.Method = “POST”
’ Posted forms need to be encoded so change the content type
Console.WriteLine(“Changing the content type (encoding)…”)
httpRequest.ContentType = “application/x-www-form-urlencoded”
’ Set the proxy
Console.WriteLine(“Setting the proxy…”)
httpRequest.Proxy = proxyServer
’ Validate and encode the data to POST
Console.WriteLine(“Validating and encode the data to POST…”)
encodedPostData = ValidatePostData(postData)
Console.WriteLine(“Encoded POST string: ‘{0}’”, encodedPostData.ToString())
’ Retrieve a byte array representation of the data
Console.WriteLine(“Retrieving a byte array representation of the data…”)
postBytes = Encoding.UTF8.GetBytes(encodedPostData.ToString())
’ Set the content length (the number of bytes in the POST request)
Console.WriteLine(“Setting the content length - the number of bytes in the POST request…”)
httpRequest.ContentLength = postBytes.Length
’ Retrieve the request stream so we can write the POST data
Console.WriteLine(“Retrieving the request stream so we can write the POST data…”)
httpPostStream = httpRequest.GetRequestStream()
’ Write the POST request
Console.WriteLine(“Writing the POST request…”)
httpPostStream.Write(postBytes, 0, postBytes.Length)
httpPostStream.Close()
httpPostStream = Nothing
’ Retrieve the response
Console.WriteLine(“Retrieving the response…”)
httpResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
’ Retrieve the response stream
Console.WriteLine(“Retrieving the response stream…”)
httpResponseStream = New BinaryReader(httpResponse.GetResponseStream(), Encoding.UTF8())
Dim readData() As Byte
’ Open the file to save the response to
Console.WriteLine(“Opening the file to save the response to…”)
localFile = File.Open(saveFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)
Console.WriteLine(“Saving response to: {0}”, localFile.Name)
Console.WriteLine(“Receiving response…”)
’ Receive the response stream until the end
Console.WriteLine(“Receiving the response stream until the end…”)
Dim count As Integer = 0
Dim percent As Long
While (True)
readData = httpResponseStream.ReadBytes(4096)
If (readData.Length = 0) Then
Exit While
End If
localFile.Write(readData, 0, readData.Length)
’ Calculate the progress and display to the console
count += readData.Length
percent = (count * 100) / httpResponse.ContentLength
’ Console.WriteLine(" “)
Console.WriteLine(”{0}% progress…", percent.ToString().PadLeft(2))
End While
Console.WriteLine()
Catch wex As WebException
Console.WriteLine(“Exception occurred: {0}”, wex.ToString())
httpResponse = CType(wex.Response, HttpWebResponse)
Finally
’ Close any remaining resources
Console.WriteLine(“Closing any remaining resources…”)
If (Not IsNothing(httpResponse)) Then
httpResponse.Close()
End If
If (Not IsNothing(localFile)) Then
localFile.Close()
End If
End Try
End Sub
’ This is the main routine that parses the command line and calls routines to
’ issue the POST request and receive the response.
’ Command line arguments
Shared Sub Main()
Dim proxyServer As IWebProxy
Dim uriToPost As String = “https://musicbrainz.org/ws/2/artist?query=artist:queen
Dim proxyName As String = Nothing
Dim postData As String = “Default.aspx?Query=web.dll”
Dim fileName As String = Nothing
’ Parse the command line
Dim args As String() = Environment.GetCommandLineArgs()
Dim i As Integer
usage()
For i = 1 To args.GetUpperBound(0)
Try
Dim CurArg() As Char = args(i).ToCharArray(0, args(i).Length)
If (CurArg(0) = “-”) Or (CurArg(0) = “/”) Then
Select Case Char.ToLower(CurArg(1), System.Globalization.CultureInfo.CurrentCulture)
Case “u”
’ URI to post to
i = i + 1
uriToPost = args(i)
Case “p”
’ Name of proxy server to use
i = i + 1
proxyName = args(i)
Case “d”
’ Retrieve all referenced images and text on the same host
i = i + 1
postData = args(i)
Case “s”
’ Local save path to append to retrieved resources
i = i + 1
fileName = args(i)
Case Else
usage()
Exit Sub
End Select
End If
Catch e As Exception
usage()
Exit Sub
End Try
Next
Try
Dim httpPost As HttpPostRequest = New HttpPostRequest()
’ Set the proxy if supplied or use the default IE static proxy
Console.WriteLine(“Setting the proxy if supplied or use the default IE static proxy…”)
If (IsNothing(proxyName)) Then
proxyServer = WebRequest.DefaultWebProxy
Else
’ Must cast it to IWebProxy if needed, not done here
proxyServer = New WebProxy(proxyName)
End If
’ Post the request and write the response to the file
Console.WriteLine(“Posting the request and write the response to the file…”)
httpPost.HttpMethodPost(uriToPost, postData, proxyServer, fileName)
Catch ex As Exception
Console.WriteLine(“Exception occurred: {0}”, ex.Message)
End Try
End Sub
End Class