Search code examples
cocoa-touchsoaprequestndfd

NDFD SOAP request in iOS


I am trying to access daily weather from the National Digital Forecast Database (NDFD) using a SOAP request. The SOAP URL is

http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php

and the Soap action for NDFDgenByDay() is

http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay. 

When I send the request, the xml I receive indicates that an error has occurred, and I believe it has to do with my HTTP headers. The error response is shown below.

 <?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-    ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SERVER</faultcode>    <faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">format needs to be either 24 hourly or 12 hourly</faultstring><detail xsi:type="xsd:string">input     format was &quot;&quot;</detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

This error objects to the "format" value, but as shown below, my xml complies.

I am sending the request in iOS/Obj-C as shown below:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"soapRequest" ofType:@"xml"];
    NSString *soapContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

    NSURL *url = [NSURL URLWithString:kWeatherSOAP_URL];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapContent length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"NDFDgenByDay" forHTTPHeaderField:@"Name"];
    [theRequest addValue:@"ndfdXMLBinding" forHTTPHeaderField:@"Binding"];
    [theRequest addValue:@"http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php" forHTTPHeaderField:@"Endpoint"];
    [theRequest addValue: @"http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay" forHTTPHeaderField:@"SoapAction"];
    [theRequest addValue:@"rpc" forHTTPHeaderField:@"Style"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapContent dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection)
        receivedData = [NSMutableData data];
    else
        NSLog(@"theConnection is NULL");

Where soapRequest.xml looks like

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
    <ns6244:NDFDgenByDay xmlns:ns6244="uri:DWMLgenByDay">
        <latitude xsi:type="xsd:string">38.99</latitude>
        <longitude xsi:type="xsd:string">-77.01</longitude>
        <startDate xsi:type="xsd:string">2012-03-12</startDate>
        <numDays xsi:type="xsd:string">7</numDays>
        <format xsi:type="xsd:string">24 hourly</format>
    </ns6244:NDFDgenByDay>
</SOAP-ENV:Body>

I am semi-confident that soapRequest.xml is correct because it is posted as sample code on the NDFD website. However, I do not know the correct way to set the HTTP headers. If anyone is familiar with this or knows what the problem is, please help me out.


Solution

  • I got your program working by changing the soapRequest.xml to the ff:

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
        <SOAP-ENV:Body>
            <ns6244:NDFDgenByDay xmlns:ns6244="uri:DWMLgenByDay">
                <latitude xsi:type="xsd:decimal">38.99</latitude>
                <longitude xsi:type="xsd:decimal">-77.01</longitude>
                <startDate xsi:type="xsd:date">2012-08-07</startDate>
                <numDays xsi:type="xsd:integer">7</numDays>
                <Unit xsi:type="xsd:string">m</Unit>
                <format xsi:type="xsd:string">24 hourly</format>
            </ns6244:NDFDgenByDay>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>