0

I'm new to web api's but do have a strong knowledge of VB.NET for website development. I'm trying to build an api that will receive xml from another api. I have my domain set up with the initial web api framework. I just need to know what function to use to accept and use the xml the other api is posting to my new api. The xml being passed is shown below. I'm trying to store these into variables which I will post to my table upon receiving them.

Any help with this would be great as I've spent days researching but can't quite search the right terms to find what I'm looking for. Thanks in advance for any responses.

Example xml being posted from external API

<exAPI>
     <client>
          <id>1</id>
          <key>1234</key>
          <ref></ref>
     </client>
</exAPI>

2 Answers 2

1

You have at least 2 options of how to do it.

But first you will need to create a class model that will represent your XML. It may look like that:

[XmlRoot("exAPI", Namespace = "")]
public class ExApi
{
    [XmlElement("client")]
    public Client Client { get; set; }
}    

public class Client
{
    [XmlElement("id")]
    public int Id { get; set; }

    [XmlElement("key")]
    public string Key { get; set; }

    [XmlElement("ref")]
    public string Ref { get; set; }
}

Automatic parameter binding. For this method to work you need to check if Content-Type: application/xml HTTP header is present in request from external API. This can be also Content-Type: text/xml. If it is, the you will need to change XmlFormatter. By default Web API uses DataContractSerializer but it will be very difficult to deserialize your XML. Just add this code to your Global.asax Application_Start method:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

And you controller method might look like that:

public void Post(ExApi exApi)
{
    // XML is automatically deserialized in exApi parameter
}

You can read more about parameter binding in this article and about custom XML serialization in this one.

Getting raw XML data from request. If Content-Type header is missing then you will need to read contents of the request manually and then parse XML:

public void Post(HttpRequestMessage req)
{
    using (var xml = req.Content.ReadAsStreamAsync().Result)
    {
        var deserializer = new XmlSerializer(typeof (ExApi));
        using (var xmlReader = XmlReader.Create(xml))
        {
            var obj = (ExApi)deserializer.Deserialize(xmlReader);
        }
    }
}

Note that method parameter is of type HttpRequestMessage which allows you to read contents of the request.

NOTE: Sorry it's not a VB.NET but I think you will manage to convert it from C#.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help, this should be a great starting point for me. No worries about the C#
0

To add to the answer on this, here is some code for making a REST GET call in VB.NET:

Private Function GetREST(ByVal Name As String) As String
    Dim content As String = Nothing
    Using client As New System.Net.Http.HttpClient
        Dim url As String = "http://some-rest.url/something/endpoint?arg1=val1&arg2=val2"
        Using request As New System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url)
            Using response As System.Net.Http.HttpResponseMessage = client.SendAsync(request).Result
            If response.IsSuccessStatusCode Then
            Dim task As System.Threading.Tasks.Task(Of System.IO.Stream) = response.Content.ReadAsStreamAsync()
            task.ContinueWith(Sub()
                        Dim result As String
                        Dim s As System.IO.Stream = task.Result
                        Using sr As New System.IO.StreamReader(s)
                            result = sr.ReadToEnd()
                        End Using
                        content = result
                    End Sub
                    ).Wait()
            End If
            End Using
        End Using
    End Using
    Return content
End Function

Couldn't find a VB.NET sample for this elsewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.