Passing header data with a URLSession in a MacOS app, getting nil response

174 Views Asked by At

I have to make a URLSession call where I pass basic auth and also some other POST data, but I am not sure if I am setting it up correctly. I am getting a nil response. The process is making a search query of a documentation database.

To make the query I have to make two calls, one to get the auth ticket, the next to pass the ticket and the search query.

I have a general URLRequest function and pass flags in for what it needs to do (read cookies, set cookies, auth, etc.) then I pass that request to a general URLSession function.

The relevant section of the request function for getting a ticket is:

//if we are getting the ticket
    if bGetTicket {
        if let dictData = dictData {
            request.httpMethod = "POST"
            
            if dictData.count > 0 {
                request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
                guard let httpBody = try? JSONSerialization.data(withJSONObject: dictData, options: []) else {
                    print("Could not  convert dictData to JSON.")
                    return
                }
                request.httpBody = httpBody
            }
        } else {
            strErrMsg.append("There was a problem with the POST data, it was nil.\n\n")
            bIsValid = false
        }//end data check
    }//bGetTicket

that works fine but when I try to do the search query I am getting a nil response:

if bIsAuth {
        if let dictData = dictData {
            request.httpMethod = "POST"
            if dictData.count > 0 {
                
                if let strTicket = dictData["ticket"] as? String, let dictQuery = dictData["dictQuery"] as? Dictionary<String, Any> {
                    request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
                    request.setValue("Basic \(strTicket)", forHTTPHeaderField: "Authorization")
                    
                    guard let httpBody = try? JSONSerialization.data(withJSONObject: dictQuery, options: []) else {
                        print("Could not  convert dictData to JSON.")
                        return
                    }
                    request.httpBody = httpBody
                }
            }
        } else {
            strErrMsg.append("There was a problem with the POST data, it was nil.\n\n")
            bIsValid = false
        }//end data check
    }//end isAuth

The format for search data is:

{
    "query": {
        "query": "dog"
    }
}

and the dictionary I am converting to JSON follows that pattern:

["query": ["query": "PSP"]]

I am getting nil on the response and when I look at the headers of the request the search data is not being attached:

Optional(["Content-Type": "Application/json", "Authorization": "Basic TICKET_184e995ed8ea09881c3ba6edcf929c03ac7e283e"])

Could someone point out where I am going wrong? Thanks!

1

There are 1 best solutions below

0
dgatwood On

Overriding the Authorization field is explicitly forbidden according to the NSURLSession documentation. It works in some situations, but not generally.

If you're doing basic auth, you must use the actual auth features of the API, or else the value you pass in will likely get overwritten.