0

Server expecting from me data in JSON format . Like this ;

enter image description here

. Im trying to send data like this ;

func getProductListResponse(typeId : String , type : String) {
    
    let tsoftFilter = [
        
        "key" : type,
        "value" : typeId
        
    
    ] as [String : AnyObject]
    
    print("tsoft filter is \(tsoftFilter)")
    
    
    let serviceParams = [
        
        "store_id" : Config.productListId,
        "page" : currentPage,
        "per_page" : perPage,
        "tsoft_filters" : "\(tsoftFilter)"
        
        
    ] as [String : AnyObject]
    

But it doesn't work where do I making mistake ?

2
  • 1
    You have a working POSTMAN sample. POSTMAN can generate Swift code. It's not beautiful Swift code, but it should give you an hint. Did you check it out? learning.postman.com/docs/sending-requests/… Commented Jan 28, 2021 at 10:42
  • I didn't know that thank you for the answer. Commented Jan 28, 2021 at 10:44

2 Answers 2

1

You are adding the description of a dictionary which is not equal to JSON. And according to the screenshot you need even an array of dictionaries.

In this case it's easier to create the JSON literally

func getProductListResponse(typeId : String , type : String) {
    
    let tsoftFilter = #"[{"key":"\(type)","value":"\(typeId)"}]"#
    
    let serviceParams : [String : Any] = [
        
        "store_id" : Config.productListId,
        "page" : currentPage,
        "per_page" : perPage,
        "tsoft_filters" : tsoftFilter
        
    ]
    ...

By the way a parameter dictionary sent in a network request is never [String:AnyObject]

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

Comments

1

Try this:

 let tsoftFilter = """
 [
    [ "key" : type, "value" : typeId]
 ] 
 """

1 Comment

JSON uses braces to indicate a dictionary, Swift does not.

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.