External API POST Request <EOL> invalid command is showing | After form submit from front-end | Springboot

77 views Asked by At

The issue I am facing that my data is successfully converted in JSON format but I am getting 500 invalid command

I Have used Sout to show the console output for debugging

Customer{first_name='jhon', last_name='smit', street='New Street', address='Wellington', city='NY', state='NY', email='[email protected]', phone='2100989877'}
[Authorization:"Bearer dGVzdEBzdW5iYXNlZGF0YS5jb206VGVzdEAxMjM=", Content-Type:"application/json"]
{"first_name":"jhon","last_name":"smit","street":"New Street","address":"Wellington","city":"NY","state":"NY","email":"[email protected]","phone":"2100989877"}
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 500: ">Invalid Command"

@Service
public class CustomerCreatingService {
    @Value("${auth.token}")
    private String authToken;

    private final RestTemplate restTemplate;

    public CustomerCreatingService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String createCustomer(String first_name, String last_name, String street, String address,
                                 String city, String state, String email, String phone) {
        // Construct the URL for API call
        String url = "https://qa2.sunbasedata.com/sunbase/portal/api/assignment.jsp";

        // Set up headers
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + authToken);
        headers.setContentType(MediaType.APPLICATION_JSON);
        System.out.println(headers);

        // Body
        Customer customer = new Customer(first_name, last_name, street, address, city, state, email, phone);
        ObjectMapper mapper = new ObjectMapper();
        String customerJson = "";
        try {
            customerJson = mapper.writeValueAsString(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return "Error serializing customer: " + e.getMessage();
        }
        HttpEntity<String> entity = new HttpEntity<>(customerJson, headers);
        System.out.println(entity.getBody());

        // Make the API call



        try {
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
            System.out.println(response);
            return "Successfully Created";
        } catch (Exception e) {
            if (e instanceof HttpClientErrorException) {
                String responseBody = ((HttpClientErrorException) e).getResponseBodyAsString();
                System.out.println("API Response: " + responseBody);
            }
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }
}

CustomerController class

    package com.example.demo.controller;

import com.example.demo.entity.Customer;
import com.example.demo.service.AuthService;
import com.example.demo.service.CustomerCreatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class CustomerController {

    private final CustomerCreatingService customerCreatingService;

    @Autowired
    public CustomerController(CustomerCreatingService customerCreatingService) {
        this.customerCreatingService = customerCreatingService;
    }

    @GetMapping("customer/create")
    public String showCreateCustomerForm() {
        return "createCustomer";  // This should map to the HTML page for creating a customer
    }

    @PostMapping("/create")
    @ResponseBody
    public String createCustomer(@ModelAttribute Customer customer) {
        System.out.println(customer);
        return customerCreatingService.createCustomer(customer.getFirst_name(),
                customer.getLast_name(),
                customer.getStreet(),
                customer.getAddress(),
                customer.getCity(),
                customer.getState(),
                customer.getEmail(),
                customer.getPhone());
    }
}

Customer class

    package com.example.demo.entity;

public class Customer {
    private String first_name;
    private String last_name;
    private String street;
    private String address;
    private String city;
    private String state;
    private String email;
    private String phone;

    // Constructor
    public Customer() {
    }

    public Customer(String first_name, String last_name, String street, String address,
                    String city, String state, String email, String phone) {
        this.first_name = first_name;
        this.last_name = last_name;
        this.street = street;
        this.address = address;
        this.city = city;
        this.state = state;
        this.email = email;
        this.phone = phone;
    }

    // Getters and setters
    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    // Optionally, you can also override `toString()` method for debugging purposes
    @Override
    public String toString() {
        return "Customer{" +
                "first_name='" + first_name + '\'' +
                ", last_name='" + last_name + '\'' +
                ", street='" + street + '\'' +
                ", address='" + address + '\'' +
                ", city='" + city + '\'' +
                ", state='" + state + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

Customer Html file

 <!DOCTYPE html>
<html>
<head>
  <title>Create Customer</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
  <h2>Create Customer</h2>
  <form action="/create" method="post">
    <div class="form-group">
      <label for="first_name">First Name*</label>
      <input type="text" class="form-control" id="first_name" name="first_name" required>
    </div>
    <div class="form-group">
      <label for="last_name">Last Name*</label>
      <input type="text" class="form-control" id="last_name" name="last_name" required>
    </div>
    <div class="form-group">
      <label for="street">Street</label>
      <input type="text" class="form-control" id="street" name="street">
    </div>
    <div class="form-group">
      <label for="address">Address</label>
      <input type="text" class="form-control" id="address" name="address">
    </div>
    <div class="form-group">
      <label for="city">City</label>
      <input type="text" class="form-control" id="city" name="city">
    </div>
    <div class="form-group">
      <label for="state">State</label>
      <input type="text" class="form-control" id="state" name="state">
    </div>
    <div class="form-group">
      <label for="email">Email</label>
      <input type="email" class="form-control" id="email" name="email">
    </div>
    <div class="form-group">
      <label for="phone">Phone</label>
      <input type="text" class="form-control" id="phone" name="phone">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</div>
</body>
</html>

0

There are 0 answers