How to correctly make post request

150 Views Asked by At

I need to make post request with my data. Also it takes access_token in header. Now I will describe the complete structure of my post-request in body. I have to send description which takes String. The data to be sent is taken from the descriptionTextView. Photo accepts a file that I upload from imagePicker in photoButton. Rate takes a dictionary. The data is taken from my text fields in rateInputView { "availability": 1, "beauty": 2, purity: 3 } Report_status always accepts the string "Accepted". Type_obj takes a string and depends on what I choose in my pickerView(route, event, place). id_obj takes the id of the object that I save from the searchBar. There should also be a Results field. Here that structure: [{ "waste_id":{"name": "Caps", "unit_of_waste": "piece"}, amount: 10 }] That takes values ​​from the text fields of the wasteInputView as well as the name of the Label in that wasteInputView.

I tried to make it, but i have an error: Failed to parse JSON: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set. around line 1, column 0." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set. around line 1, column 0., NSJSONSerializationErrorIndex=0}

How i can fix it? Here is my code:

@objc func postActionButton() {
        // Check if access token is available
        guard let accessToken = UserDefaults.standard.string(forKey: "AccessToken") else {
            print("Access token is missing")
            return
        }
        
        // Prepare the URL
        guard let url = URL(string: "\(apiLink)/report/create_report/") else {
            print("Invalid URL")
            return
        }
        
        // Prepare the request
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        
        // Set the access token in the header
        request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
        
        // Prepare the data for the request body
        guard let image = selectedImage else {
            print("No image selected")
            return
        }
        
        guard let imageData = image.jpegData(compressionQuality: 0.8) else {
            print("Failed to convert image to data")
            return
        }
        
        let base64Image = imageData.base64EncodedString()
        
        // Create the request body
            let parameters: [String: Any] = [
                "photo": base64Image,
                "description": descriptionTextView.text ?? "",
                "rate": [
                    "availability": rateInputView.transportTextField.text ?? "",
                    "beauty": rateInputView.beautyTextField.text ?? "",
                    "purity": rateInputView.pollutionTextField.text ?? ""
                ],
                "report_status": "Accepted",
                "type_obj": selectedOption ?? "",
                "id_obj": selectedOptionID ?? 0,
                "Results": [
                    [
                        "waste_id": [
                            "name": wasteInputView.wasteLabel.text ?? "",
                            "unit_of_waste": "kg"
                        ],
                        "amount": wasteInputView.bulbTextField.text ?? ""
                    ] as [String : Any]
                ]
            ]
            
            do {
                request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
            } catch {
                print("Failed to create JSON data: \(error)")
                return
            }

            let task = URLSession.shared.dataTask(with: request) { data, response, error in
                if let error = error {
                    print("Request error: \(error)")
                    return
                }

                guard let data = data else {
                    print("No data received")
                    return
                }

                do {
                    if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
                        // Process the response JSON
                        print("Response JSON: \(json)")
                    }
                } catch {
                    print("Failed to parse JSON: \(error)")
                }
            }

            task.resume()
    }

Here's stucture in postman: enter image description here enter image description here

0

There are 0 best solutions below